//
//  RecFastCell.m
//  CarFace
//
//  Created by dlios on 15/4/1.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RecFastCell.h"

#import "Attachments.h"
#import "Commentlist.h"

@interface RecFastCell()

@property(nonatomic, retain)UIImageView *headView;
@property(nonatomic, retain)UILabel *authornameLabel;
@property(nonatomic, retain)UILabel *contentLabel;
@property(nonatomic, retain)UIImageView *attachments;
@property(nonatomic, assign)BOOL flag;


          
@end

@implementation RecFastCell
- (void)dealloc
{
    [_headView release];
    [_authornameLabel release];
    [_contentLabel release];
    [_attachments release];
    [_mess release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGFloat widths = self.contentView.frame.size.width;
    
        self.headView = [[UIImageView alloc] initWithFrame:CGRectMake( widths / 50, widths / 50, widths / 8, widths / 8)];
        self.headView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"用户.png"]];
        [self.contentView addSubview:self.headView];
        [_headView release];
        
        self.authornameLabel = [[UILabel alloc] initWithFrame:CGRectMake(widths * 3 / 12, widths / 40, widths / 2, widths / 12) ];
        self.authornameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.authornameLabel];
        [_authornameLabel release];
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(widths / 50, self.headView.frame.size.height + widths * 2 / 50, widths * 48 / 50, 40)];
        [self.contentView addSubview:self.contentLabel];
        [_contentLabel release];
        self.flag = YES;
        for (NSInteger i = 0; i < 20; i++) {
            UIImageView *imageViewTen = [[UIImageView alloc] init];
            [self.contentView addSubview:imageViewTen];
            imageViewTen.tag = 3000 + i;
            imageViewTen.contentMode = UIViewContentModeScaleToFill;
            [imageViewTen release];
        }
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.contentView.frame.size.width;
    self.contentLabel.frame = CGRectMake(width / 50, self.headView.frame.size.height + width * 2 / 50, width * 48 / 50, 40);
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel sizeToFit];
    for (NSInteger i = 0; i < self.mess.att.count ; i++) {
        Attachments *att = self.mess.att[i];
        UIImageView *image = (UIImageView *)[self viewWithTag:3000 + i];
        UIImageView *imageBack = (UIImageView *)[self viewWithTag:2999 + i];
        CGFloat hei = [att.height floatValue];
        CGFloat wid = [att.width floatValue];
        CGFloat photoHeigth = width * 48 / 50 * hei / wid;
        image.frame = CGRectMake(width / 50, imageBack.frame.size.height + imageBack.frame.origin.y + width / 50 , width * 48 / 50, photoHeigth);
        if (i == 0) {
            image.frame = CGRectMake(width / 50, (self.contentLabel.frame.origin.y + width  / 50 + self.contentLabel.frame.size.height), width * 48 / 50, photoHeigth);
        }
    }
    NSString *str = [self.mess headimg];
    NSURL *url = [NSURL URLWithString:str];
    //        NSLog(@"url     %@", url);
    [self.headView setImageWithURL:url];

}

- (void)setMess:(Messagelist *)mess
{
    if (_mess != mess) {
        [_mess release];
        _mess = [mess retain];
    }
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel sizeToFit];
    NSArray * contentArr = [self.contentView subviews];
    //判断当不是self.headView的时候, 将其它重用的图片frame清零;
    for (id class in contentArr) {
        if ([class isKindOfClass:[UIImageView class]]) {
            UIImageView *image = class;
            if (image == self.headView) {
                
            } else {
                image.frame = CGRectZero;
            }
        }
    }
    
    self.authornameLabel.text = self.mess.authorname;
    self.contentLabel.text = self.mess.content;
     for (NSInteger i = 0; i < self.mess.att.count ; i++) {
         Attachments *att = self.mess.att[i];
         UIImageView *image = (UIImageView *)[self viewWithTag:3000 + i];
         NSString *str = [att picurl];
         str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
         NSURL *url = [NSURL URLWithString:str];
         [image setImageWithURL:url];
     }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
